﻿Public Class frmEmployee

    Private Sub frmEmployee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        getHeaderGrid_Employee()
        getEmployee()
        reset()
    End Sub

    Protected Sub getHeaderGrid_Employee()
        Dim HeaderText As String() = {"លេខរៀង", "ID", "ឈ្មោះបុគ្គលិក", "ភេទ", "ថ្ងៃខែឆ្នាំកំណើត", "អាសយដ្ឋាន", "លេខទូរស័ព្ទ", "អ៊ីម៉ែល", "តួនាទី"}
        Dim HeaderWidth As Integer() = {100, 100, 250, 100, 150, 400, 150, 200, 200}
        Dim i As Integer
        dgvData.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            dgvData.Columns(i).HeaderText = HeaderText(i)
            dgvData.Columns(i).Width = HeaderWidth(i)
        Next
        dgvData.Columns(1).Visible = False
        dgvData.Columns(4).DefaultCellStyle.Format = "dd/MM/yyyy"
    End Sub

    Public Sub getEmployee()
        dgvData.Rows.Clear()
        Dim oEmployee As New cEmployee
        Dim dt As New DataTable
        Dim dr As DataRow
        Dim i As Integer = 1

        dt = oEmployee.select_Employee()
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(1), dr(2), dr(3), dr(4), dr(5), dr(6), dr(7))
            i += 1
        Next
    End Sub

    Private Sub ShowFormWithoutClose(ByVal frm As Form)
        frm.MdiParent = Me.MdiParent
        frm.ControlBox = False
        frm.ShowIcon = False
        frm.WindowState = FormWindowState.Maximized
        frm.Show()
        frm.BringToFront()
    End Sub

    Protected Sub reset()
        txtSearch.Clear()
        txtSearch.Focus()
    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        getSearch()
    End Sub

    Protected Sub getSearch()
        dgvData.Rows.Clear()
        Dim oEmployee As New cEmployee
        Dim dt As New DataTable
        Dim dr As DataRow
        Dim i As Integer = 1

        dt = oEmployee.select_Employee_byKeyword(txtSearch.Text)
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(1), dr(2), dr(3), dr(4), dr(5), dr(6), dr(7))
            i += 1
        Next
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
        If dgvData.Rows.Count > 0 Then
            frmEmployeeEntry.lblID.Text = dgvData.CurrentRow.Cells(1).Value
            frmEmployeeEntry.EnableControl()
            ShowFormWithoutClose(frmEmployeeEntry)
        Else
            MessageBox.Show("គ្មានពត៌មានសម្រាប់់បង្ហាញទេ!", "បង្ហាញពត៌មាន", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        ShowFormWithoutClose(frmEmployeeEntry)
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        If dgvData.Rows.Count > 0 Then
            frmEmployeeEntry.lblID.Text = dgvData.CurrentRow.Cells(1).Value
            ShowFormWithoutClose(frmEmployeeEntry)
        Else
            MessageBox.Show("គ្មានពត៌មានសម្រាប់់កែប្រែទេ!", "កែប្រែ", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        deleted()
    End Sub

    Protected Sub deleted()
        Dim oEmployee As New cEmployee
        If dgvData.Rows.Count > 0 Then
            If MessageBox.Show("តើអ្នកប្រាកដជាលុប '" & dgvData.CurrentRow.Cells(2).Value & "' មែនទេ?", "លុបចោល", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                If oEmployee.delete_Employee(dgvData.CurrentRow.Cells(1).Value) Then
                    MessageBox.Show("លុបចោលបានសម្រេច!", "លុបចោល", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    getEmployee()
                Else
                    MessageBox.Show("លុបចោលមិនបានសម្រេចទេ", "លុបចោល", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            End If
        Else
            MessageBox.Show("គ្មានពត៌មានសម្រាប់់លុបទេ!", "លុបចោល", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub dgvData_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvData.DoubleClick
        btnUpdate_Click(Nothing, Nothing)
    End Sub
End Class